package com.imstlife.vending.adapter;


import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.imstlife.vending.R;

public class SettingsItemAdapter extends BaseAdapter {
	private Context context;
	// 得到一个LayoutInfalter对象用来导入布局
	private LayoutInflater mInflater;
	private String[] itemTitles;
	private int selectItem = -1;

	/**
	 * 构造函数
	 */
	public SettingsItemAdapter(Context context) {
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		itemTitles = new String[] {
				"  WLAN",
				"  " + context.getResources().getString(R.string.date_and_time),
				"  " + context.getResources().getString(R.string.more),
				"  " + context.getResources().getString(R.string.manage),
				"  " + context.getResources().getString(R.string.tradecheck),
				"  " + context.getResources().getString(R.string.tradecheckall),
				"  " + context.getResources().getString(R.string.about) };
	}

	@Override
	public int getCount() {
		int count = 0;

		if (itemTitles != null) {
			count = itemTitles.length;
		}

		return count;
	}

	@Override
	public Object getItem(int position) {
		if (itemTitles == null) {
			return null;
		}

		return itemTitles[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item_settings_item,
					null);
			holder = new ViewHolder();
			// 得到各个控件的对象
			holder.layout = (LinearLayout) convertView
					.findViewById(R.id.settings_item_layout);
			holder.textTv = (TextView) convertView
					.findViewById(R.id.settings_item_tv);

			convertView.setTag(holder);
		} else {
			// 取出ViewHolder对
			holder = (ViewHolder) convertView.getTag();
		}

		if (selectItem == position) {
			int color = 0x5f02403a;
			holder.layout.setBackgroundColor(color);
		} else {
			holder.layout.setBackgroundColor(Color.TRANSPARENT);

		}

		holder.textTv.setText(itemTitles[position]);

		return convertView;
	}

	/** 存放控件 */
	public final class ViewHolder {
		public LinearLayout layout;
		public TextView textTv;
	}

	public void setSelectItem(int selectItem) {
		this.selectItem = selectItem;
	}
}
